Imports UniversalLayer.ULMain

Public Class ULDataHandler

#Region "Data Handling"

    ''' <summary>
    ''' NO COMMENTS
    ''' </summary>
    ''' <param name="sList"></param>
    ''' <param name="sEachItem"></param>
    ''' <param name="cDelimeter">Delimeter (default: comma)</param>
    ''' <param name="sPrefix">Prefix to be appended before item</param>
    ''' <param name="sSuffix">Suffix to be appended after item</param>
    ''' <remarks></remarks>
    Public Shared Sub AddItemsToList(ByRef sList As String, ByRef sEachItem As String, _
    Optional ByVal cDelimeter As Char = ","c, Optional ByVal sPrefix As String = "'", Optional ByVal sSuffix As String = "'")
        If sList.Trim.Length = 0 Then
            sList = sPrefix & sEachItem.Trim & sSuffix
            Exit Sub
        End If

        sList = sList & cDelimeter & sPrefix & sEachItem & sSuffix
    End Sub

    ''' <summary>
    ''' NO COMMENTS
    ''' </summary>
    ''' <param name="sList"></param>
    ''' <param name="arrItems"></param>
    ''' <param name="cDelimeter">Delimeter</param>
    ''' <param name="sPrefix">Prefix to be appended before item</param>
    ''' <param name="sSuffix">Suffix to be appended after item</param>
    ''' <remarks></remarks>
    Public Shared Sub AddArrayToList(ByRef sList As String, ByRef arrItems() As String, _
    Optional ByVal cDelimeter As Char = ","c, Optional ByVal sPrefix As String = "'", Optional ByVal sSuffix As String = "'")
        For Each eachItem As String In arrItems
            AddItemsToList(sList, eachItem, cDelimeter, sPrefix, sSuffix)
        Next
    End Sub

    ''' <summary>
    ''' Copies one DataTable into another (with data or only structure)
    ''' </summary>
    ''' <param name="dtSource">Source DataTable</param>
    ''' <param name="dtTarget">Target DataTable</param>
    ''' <param name="sColumns">Columns to be copied (comma-separated)</param>
    ''' <param name="sFilterCondition">Filter condition</param>
    ''' <param name="bDistinct">TRUE: Only distinct records are returned</param>
    ''' <param name="sTargetTableName">Target table name</param>
    ''' <param name="sSort">Sort parameters</param>
    ''' <param name="bCopyStructureOnly">True: Copy one table structure without data; False (default)</param>
    ''' <remarks></remarks>
    Public Shared Sub CopyDataTable(ByVal dtSource As DataTable, ByRef dtTarget As DataTable, _
    Optional ByVal sFilterCondition As String = "", Optional ByVal sColumns As String = "", _
    Optional ByVal sTargetTableName As String = "", Optional ByVal bDistinct As Boolean = True, _
    Optional ByVal sSort As String = "", Optional ByVal bCopyStructureOnly As Boolean = False)
        'If IsEmpty(dtTarget) Then
        '    If bCopyTableName Then dtTarget = New DataTable(dtSource.TableName) _
        '    Else dtTarget = New DataTable()
        'End If

        Dim arrColumns() As String
        Dim dvFilteredView As DataView
        Try
            If bCopyStructureOnly Then
                dtTarget = dtSource.DefaultView.ToTable(sTargetTableName)
                dtTarget.Rows.Clear()
                Exit Sub
            End If

            If IsEmpty(sColumns) Then
                For Each dc As DataColumn In dtSource.Columns
                    'ReDim Preserve arrColumns(arrColumns.Length)
                    'arrColumns(arrColumns.Length - 1) = dc.ColumnName
                    'sColumns = CStr(IIf(IsEmpty(sColumns), "", sColumns & ",")) & dc.ColumnName
                    AddItemsToList(sColumns, dc.ColumnName, , "", "")
                Next
            End If
            arrColumns = sColumns.Split(","c)

            If IsEmpty(sSort) Then
                ''For Each dc As DataColumn In dtSource.Columns
                ''    'AddItemsToList(sSort, dc.ColumnName, , " ", " ASC")
                ''    AddArrayToList(sSort, arrColumns, , " ", " ASC")
                ''Next
                If dtSource.PrimaryKey.Length > 0 Then _
                    sSort = dtSource.PrimaryKey(0).ColumnName & " ASC"
            End If

            'If IsEmpty(sSort) Then sSort = dtSource.DefaultView.Sort

            If IsEmpty(sFilterCondition) Or sFilterCondition = "(1=1)" Then
                If IsEmpty(sTargetTableName) Then dtTarget = dtSource.DefaultView.ToTable(bDistinct, arrColumns) _
                    Else dtTarget = dtSource.DefaultView.ToTable(sTargetTableName, bDistinct, arrColumns)
            Else
                dvFilteredView = New DataView(dtSource, sFilterCondition, sSort, DataViewRowState.CurrentRows)
                dtTarget = dvFilteredView.ToTable(sTargetTableName, bDistinct, arrColumns)
                'dvFilteredView = Nothing
            End If

        Finally
            Erase arrColumns
            dvFilteredView = Nothing
        End Try
    End Sub

#End Region
End Class
